package main

import (
	"filedemo/service"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/lxn/win"

	"github.com/lxn/walk"

	. "github.com/lxn/walk/declarative"
)

type MyMainWindow struct {
	*walk.MainWindow
	SelectDirBtn    *walk.PushButton
	DirPathLineEdit *walk.LineEdit
	SearchBtn       *walk.PushButton
	SearchContent   *walk.TextEdit
	SearchResult    *walk.TextEdit
	ExportBtn       *walk.PushButton
}

var dir string

func main() {
	var mw = &MyMainWindow{
		SelectDirBtn:    &walk.PushButton{},
		DirPathLineEdit: &walk.LineEdit{},
		SearchBtn:       &walk.PushButton{},
		SearchContent:   &walk.TextEdit{},
		SearchResult:    &walk.TextEdit{},
		ExportBtn:       &walk.PushButton{},
	}

	if err := (MainWindow{
		AssignTo: &mw.MainWindow,
		Title:    "文件搜索",
		MinSize:  Size{400, 300},
		Size:     Size{800, 600},
		Font: Font{
			PointSize: 10,
		},
		Layout: VBox{},
		Children: []Widget{
			GroupBox{
				Layout: HBox{},
				Children: []Widget{
					PushButton{
						AssignTo: &mw.SelectDirBtn,
						Text:     "选择文件夹",
						MaxSize: Size{
							Width:  170,
							Height: 30,
						},
						OnClicked: mw.SelectDir,
					},
					Label{
						Text: "选中文件夹：",
					},
					LineEdit{
						AssignTo: &mw.DirPathLineEdit,
						Text:     dir,
					},
				},
			},
			GroupBox{
				Layout: HBox{},
				Children: []Widget{
					Label{
						Text: "请输入要搜索的内容：",
					},
					PushButton{
						AssignTo: &mw.SearchBtn,
						Text:     "搜索",
						MaxSize: Size{
							Width:  170,
							Height: 30,
						},
						Alignment: AlignHFarVNear,
						OnClicked: mw.StarSearch,
						Background: SystemColorBrush{
							Color: walk.SysColorInfoBk,
						},
					},
				},
			},
			TextEdit{
				AssignTo: &mw.SearchContent,
				VScroll:  true,
				Font: Font{
					PointSize: 12,
				},
			},
			Label{
				Text: "搜索结果：",
			},
			TextEdit{
				AssignTo: &mw.SearchResult,
				VScroll:  true,
				Font: Font{
					PointSize: 12,
				},
			},
			GroupBox{
				Layout: HBox{},
				Children: []Widget{
					PushButton{
						AssignTo: &mw.ExportBtn,
						Text:     "导出结果",
						MaxSize: Size{
							Width:  170,
							Height: 30,
						},
						Alignment: AlignHFarVNear,
						OnClicked: mw.ExportResult,
					},
				},
			},
		},
	}.Create()); err != nil {
		log.Fatal(err)
	}
	// 设置窗体在屏幕的正中间
	mw.SetX((int(win.GetSystemMetrics(0)) - mw.Width()) / 2)
	mw.SetY((int(win.GetSystemMetrics(1)) - mw.Height()) / 2)
	mw.Run()
}

// SelectDir 选择文件夹
func (m *MyMainWindow) SelectDir() {
	f := new(walk.FileDialog)
	folder, err := f.ShowBrowseFolder(m)
	if !folder {
		// 选择了取消，没有选择文件夹会进入到这里
		fmt.Printf("选择文件夹出错 %v \n", err)
		//walk.MsgBox(m, "提示", "选择文件夹出错", walk.MsgBoxIconInformation)
		return
	}
	err = m.DirPathLineEdit.SetText(f.FilePath)
	if err != nil {
		walk.MsgBox(m, "提示", "设置选中文件夹出错", walk.MsgBoxIconInformation)
		return
	}
	fmt.Println("选择文件夹成功", f.FilePath)
}

func (m *MyMainWindow) StarSearch() {
	// 判断文件夹不能为空
	path := strings.TrimSpace(m.DirPathLineEdit.Text())
	if path == "" {
		walk.MsgBox(m, "提示", "搜索文件夹不能为空", walk.MsgBoxIconInformation)
		return
	}
	// 校验搜索内容不能为空
	searchContent := m.SearchContent.Text()
	searchContent = strings.TrimSpace(searchContent)
	if searchContent == "" {
		walk.MsgBox(m, "提示", "搜索内容不能为空", walk.MsgBoxIconInformation)
		return
	}
	// 将搜索内容转为数据
	fmt.Println(searchContent)
	split := strings.Split(searchContent, "\r\n")
	// 开始搜索文件
	// 先清空之前的搜索结果
	m.SearchResult.SetText("结果搜索中......")
	go func() {
		list := service.FindByList(path, split)
		var searchResult string = ""
		for _, v := range list {
			searchResult += v + "\r\n"
		}
		// 将结果保存到输出结果框中
		if searchResult == "" {
			searchResult = "没有查询到结果"
		}
		err := m.SearchResult.SetText(searchResult)
		if err != nil {
			fmt.Println("设置搜索结果出错")
			walk.MsgBox(m, "提示", "设置搜索结果出错", walk.MsgBoxIconInformation)
			return
		}
	}()
}

func (m *MyMainWindow) ExportResult() {
	f := new(walk.FileDialog)
	f.Title = "搜索结果"
	f.Filter = ".txt"
	_, err := f.ShowSave(m)
	fmt.Printf("%+v \n", f)
	if err != nil {
		fmt.Println(err)
		return
	}
	if f.FilePath == "" {
		return
	}
	file, err := os.OpenFile(f.FilePath+".txt", os.O_CREATE|os.O_APPEND, 0777)
	if err != nil {
		fmt.Println("文件生成失败")
		return
	}
	defer file.Close()
	file.Write([]byte(m.SearchResult.Text()))
}
